/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 * A simple data model for keeping huge data set. It saves memory since there is only single Cell object which refer to the data model for obtaining data for that cell. This is the major advantage of the model
 *  
 * @author: jin Chen 
 * @date: Dec 6, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.gmatrix.data;

import edu.psu.geovista.app.gmatrix.canvas.Cell;
import edu.psu.geovista.app.gmatrix.canvas.RectangleSymbol;
import edu.psu.geovista.app.gmatrix.canvas.Symbol;
import edu.psu.geovista.app.gmatrix.toolkit.CellFactory;

import java.awt.*;

public class LightMatrixmodel implements BasicMatrixmodel {
    private int numOfRow;
    private int numOfCulumn;
    Color defaultForeground=Color.gray;
    Color defaultBackground=Color.black;
    private Color[][] foregrounds; // 
    private Color[][] backgrounds; // 
    Symbol defaultSymbol=new RectangleSymbol();
    private Symbol[][] symbols;

    CellFactory cellfactory;
    //EventFirer eventfire;
    public LightMatrixmodel() {
         this.cellfactory=new CellFactory();
        //eventfire=new EventFirer();
    }

    /*******************************************************************************************************
        *      important setting
        *******************************************************************************************************/
       public void setColors(Color[][] colors) {
            setNumOfRow(colors.length) ;
           setNumOfCulumn(colors[0].length) ;
           this.setForegrounds(colors);
          // this.eventfire.fireDataChanged();
       }

       public void setColors(Color[] colors) {

           // this.setNumOfRow(1) ;
          // this.setNumOfCulumn(colors.length) ;
           Color[][] mycolors=new Color[1][colors.length ];
           mycolors[0]=colors;
           setColors(mycolors);
            //this.eventfire.fireDataChanged();
       }
        public void setRowColumn( int row, int col){
           this.setNumOfRow(row) ;
           this.setNumOfCulumn(col);
           this.setForegrounds(null);//replace current cell colors
           // this.eventfire.fireDataChanged();
           //repaint();
       }
       public Color[] getColumnColors(int index) {

           Color[][] colors = this.getForegrounds();
           int numOfColumn = colors[0].length;
           if(index>=numOfColumn){
               assert(false );
           }
           int numOfRow = colors.length;
           Color[] columnColors=new Color[numOfRow] ;
           for (int i=0;i<numOfRow ;i++){
               int row=i;
               Color[] rowcolors=colors[row];
               columnColors[row]=rowcolors[index];
           }
           return columnColors;

       }

       public Color[] getRowColors(int index) {

            Color[][] colors = this.getForegrounds();
           return colors[index];
       }

       /**
     *
     * @param row
     * @param col
     * @return    a symbol for a row/col index
     */
       public Symbol getSymbol(int row, int col) {

             if(getSymbols() ==null) return this.getDefaultSymbol() ;
           Symbol symbol = getSymbols()[row][col];
           if(symbol==null) return this.getDefaultSymbol() ;
           return symbol;
       }

    public  Cell getCell(int row, int col) {
        Symbol symbol = this.getSymbol(row, col);
        Cell cell = this.cellfactory.createCell(symbol);

        // 
        cell.setIndex(row,col);
        cell.setBackground(this.getBackground(row,col) );
        cell.setForeground(this.getForeground(row,col));


        return cell;
    }


    public Color getForeground(int row, int col) {
         if(getForegrounds() ==null) return defaultForeground;
        else
            return getForegrounds()[row][col];
    }



    public Color getBackground(int row, int col) {

         if(getBackgrounds() ==null) return defaultBackground;
        else
            return getBackgrounds()[row][col];

    }

    /*******************************************************************************************************
     *                setter/getter
     *******************************************************************************************************/
    public Color[][] getBackgrounds() {
           return backgrounds;
       }

       public void setBackgrounds(Color[][] backgrounds) {
           this.backgrounds = backgrounds;
       }

    public Color[][] getForegrounds() {
        return foregrounds;
    }

    protected  void setForegrounds(Color[][] foregrounds) {
        this.foregrounds = foregrounds;
    }

    public int getNumOfCulumn() {
        return numOfCulumn;
    }

    public void setNumOfCulumn(int numOfCulumn) {
        this.numOfCulumn = numOfCulumn;
    }

    public int getNumOfRow() {
        return numOfRow;
    }

    public void setNumOfRow(int numOfRow) {
        this.numOfRow = numOfRow;
    }
    public Symbol[][] getSymbols() {
        return symbols;
    }
    public void setSymbols(Symbol[][] symbols) {
        this.symbols = symbols;
    }
    /*******************************************************************************************************
     *                default values
     *******************************************************************************************************/
     public Color getDefaultBackground() {
        return defaultBackground;
    }

    public void setDefaultBackground(Color defaultBackground) {
        this.defaultBackground = defaultBackground;
    }

    public Color getDefaultForeground() {
        return defaultForeground;
    }

    public void setColumnIdentifiers(Object[] names) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void setRowIdentifiers(Object[] names) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void setDefaultForeground(Color defaultForeground) {
        this.defaultForeground = defaultForeground;
    }

    public Symbol getDefaultSymbol() {
        return defaultSymbol;
    }

    public void setDefaultSymbol(Symbol defaultSymbol) {
        this.defaultSymbol = defaultSymbol;
    }
/*

    public void addTableModelListener(TableModelListener l) {
	eventfire.addListener(TableModelListener.class, l);
    }
    class EventFirer {
        DefaultTableModel md=new DefaultTableModel();//todo temp only for fire event purpose
        protected EventListenerList listenerList = new EventListenerList();

        public void addListener(Class t, EventListener l) {

            this.listenerList.add(t,l);
        }
        public void removeListener(Class t, EventListener l) {
            this.listenerList.remove(t,l);
        }

         public void fireDataChanged() {
            TableModelEvent e=new TableModelEvent()
            Object[] listeners=this.listenerList.getListenerList() ;
            //TSEvent e=null;
            for (int i = listeners.length-2; i>=0; i-=2) {
	    if (listeners[i]== TableModelListener.class) {
		((TableModelListener)listeners[i+1]).tableChanged(e);
	    }
	}
        }


    }*/

}
